Skip to content

Deterministic MoE Router Kernel By Removing Global Atomic Ops - #3587

Merged
vthumbe1503 merged 5 commits into
NVIDIA:mainfrom
zhongbozhu:fix/deterministic-moe-aux-loss
Oct 30, 2026
Merged

vthumbe1503 merged 5 commits into
NVIDIA:mainfrom
zhongbozhu:fix/deterministic-moe-aux-loss

Conversation

@zhongbozhu

@zhongbozhu zhongbozhu commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Please include a brief summary of the changes, relevant motivation and context.

Fixes NVIDIA/Megatron-LM#7221

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Please list the changes introduced in this PR:

  • Change A
  • Change B

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Sep 29, 2026
@greptile-apps

greptile-apps Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The reviewed changes appear safe to merge; no blocking issue was found.

Summary

Adds an optional fixed-order reduction for MoE auxiliary loss while keeping the existing atomic path as the default.

  • MoE auxiliary loss can use a fixed-order reduction.
  • The fused MLA module keeps its imports in source order.

No actionable new issues were found. Tests were not run. No previous review threads were supplied, and their history could not be retrieved.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[fused_moe_aux_loss] --> B{deterministic?}
  B -->|No| C[Existing atomic reduction]
  B -->|Yes| D[Get workspace]
  D --> E[Each block writes one partial sum]
  E --> F[Fixed-order final sum]
  C --> G[Loss and saved backward coefficient]
  F --> G
Loading

Reviews (7) · Last reviewed commit: "Merge branch 'main' into fix/determinist..." · Reviewed by Greptile

*
* \return Maximum required workspace size in bytes.
*/
size_t nvte_get_moe_aux_loss_workspace_size();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way we handle the workspace in the other APIs is similar to CUB - we run the APIs with the nullptr arguments for the workspace once (which populates the other fields), then run it again with the proper workspace. Please align this API to that.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@ZhiyuLi-Nvidia

Copy link
Copy Markdown
Contributor

Thanks a lot @zhongbozhu.

The kernel is deterministic on one GPU. For multi-node runs, does determinism also hold with multi-node job, do we need the same placement and a fixed NCCL algorithm? Good to put it in the docs if there's anything worth noting.

@zhongbozhu
zhongbozhu force-pushed the fix/deterministic-moe-aux-loss branch from 17c35fa to 07887f8 Compare October 15, 2026 07:29
@zhongbozhu

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch

@zhongbozhu
zhongbozhu force-pushed the fix/deterministic-moe-aux-loss branch from 07887f8 to a5d4c78 Compare October 19, 2026 23:12
@ZhiyuLi-Nvidia

Copy link
Copy Markdown
Contributor

Verified on my end:

@zhongbozhu

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch

@vthumbe1503 vthumbe1503 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with minor suggestion on workspace contract

import transformer_engine_torch as tex
from packaging.version import Version as PkgVersion

import transformer_engine_torch as tex

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file change is not needed right?

@zhongbozhu zhongbozhu Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that seems to be linter complaint?

Signed-off-by: Zhongbo Zhu <zhongboz@nvidia.com>
Signed-off-by: zhongboz <zhongboz@nvidia.com>
Query workspace shape and dtype through the deterministic forward APIs, retain stream and CUDA Graph isolation, and cover metadata queries and invalid scratch buffers. Fix the stream cache-key lint warning and upstream import ordering.

Validated on GB200: 201 TE tests passed (12 invalid top-k cases skipped), 6 C API tests passed, and 40 MCore checks passed with 1 expected atomic negative-control XPASS. Full PyTorch QA lint, pre-commit, and license checks passed.

Signed-off-by: zhongboz <zhongboz@nvidia.com>
Signed-off-by: zhongboz <zhongboz@nvidia.com>
@zhongbozhu
zhongbozhu force-pushed the fix/deterministic-moe-aux-loss branch from a5d4c78 to 23ce45f Compare October 28, 2026 07:05
Apply Varun Thumbe's workspace-contract changes from
#1. Require the queried 256-element FP32
capacity for every input shape and update the C API workspace tests.
Retain the existing MLA import grouping required by Python lint.

Original author: Varun Thumbe <vthumbe@nvidia.com>
Source: 9a3e764

Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Signed-off-by: zhongboz <zhongboz@nvidia.com>
@zhongbozhu

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch

@vthumbe1503

Copy link
Copy Markdown
Collaborator

/te-ci L0

@vthumbe1503

Copy link
Copy Markdown
Collaborator

Relevant tests are passing. Pipeline: 77803646

@vthumbe1503
vthumbe1503 merged commit fd2ab0c into NVIDIA:main Oct 30, 2026
33 of 38 checks passed
fheinecke pushed a commit that referenced this pull request Oct 30, 2026
* initial draft

Signed-off-by: Zhongbo Zhu <zhongboz@nvidia.com>
Signed-off-by: zhongboz <zhongboz@nvidia.com>

* Align deterministic MoE aux-loss workspace queries with the common API

Query workspace shape and dtype through the deterministic forward APIs, retain stream and CUDA Graph isolation, and cover metadata queries and invalid scratch buffers. Fix the stream cache-key lint warning and upstream import ordering.

Validated on GB200: 201 TE tests passed (12 invalid top-k cases skipped), 6 C API tests passed, and 40 MCore checks passed with 1 expected atomic negative-control XPASS. Full PyTorch QA lint, pre-commit, and license checks passed.

Signed-off-by: zhongboz <zhongboz@nvidia.com>

* Test deterministic MoE aux-loss integer boundaries

Signed-off-by: zhongboz <zhongboz@nvidia.com>

* Simplify deterministic MoE aux-loss workspace contract

Apply Varun Thumbe's workspace-contract changes from
zhongbozhu#1. Require the queried 256-element FP32
capacity for every input shape and update the C API workspace tests.
Retain the existing MLA import grouping required by Python lint.

Original author: Varun Thumbe <vthumbe@nvidia.com>
Source: zhongbozhu@9a3e764

Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Signed-off-by: zhongboz <zhongboz@nvidia.com>

---------

Signed-off-by: Zhongbo Zhu <zhongboz@nvidia.com>
Signed-off-by: zhongboz <zhongboz@nvidia.com>
Signed-off-by: Varun Thumbe <vthumbe@nvidia.com>
Co-authored-by: Varun Thumbe <vthumbe@nvidia.com>
Co-authored-by: Przemyslaw Tredak <ptredak@nvidia.com>
(cherry picked from commit fd2ab0c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.21 community-contribution PRs from external contributor outside the core maintainers, representing community-driven work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Determinism] Add deterministic TE fused loss support

4 participants